package com.hwbsite.hwbback.utils;

import com.hwbsite.hwbback.entity.HwbUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class UserTokenUtils {

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static HwbUser getUser(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        return principal instanceof HwbUser ? (HwbUser)principal : null;
    }

    public static HwbUser getUser() {
        Authentication authentication = getAuthentication();
        return authentication == null ? null : getUser(authentication);
    }

    public static HwbUser getUserId() {
        HwbUser hwbUser = getUser();
        if (hwbUser == null) {
            return null;
        } else {
            return hwbUser;
        }
    }
}
